﻿using FunArts.EasyDevOps.Shared.Models.SshClient;
using FunArts.EasyDevOps.Shared.Models.SshClient.CommandResult;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FunArts.EasyDevOps.Rcl.Service
{
    public interface ISshClientService
    {
        /// <summary>
        /// 连接信息
        /// </summary>
        public SshClientConnectInfo SshClientConnectInfo { get; set; }
        /// <summary>
        /// 是否连接
        /// </summary>
        public bool IsConnected { get; }
        /// <summary>
        /// 连接
        /// </summary>
        /// <param name="errMsg"></param>
        /// <returns></returns>
        bool Connect(out string errMsg);
        /// <summary>
        /// 断开连接
        /// </summary>
        void Disconnect();

        /// <summary>
        /// 执行命令
        /// </summary>
        /// <typeparam name="T">命令类型</typeparam>
        /// <param name="command"></param>
        /// <returns></returns>
        LinuxCommandResultBase ExecuteCommand<T>(T command) where T : ILinuxCommand;
        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="createCommandFunc"></param>
        /// <returns></returns>
        LinuxCommandResultBase ExecuteCommand(Func<ILinuxCommand> createCommandFunc);
        /// <summary>
        /// 执行命令，返回具体结果类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="R"></typeparam>
        /// <param name="command"></param>
        /// <returns></returns>
        public virtual LinuxCommandResultBase<List<R>> ExecuteCommand<T,R>(T command) where T : ILinuxCommand where R : ILinuxCommandResultData<R>,new()
        {
            var resultBase= ExecuteCommand<T>(command);
            R r=new R();
            return r.ParseResultDataList(resultBase);
        }
        /// <summary>
        /// 执行命令，返回具体类型
        /// </summary>
        /// <typeparam name="R"></typeparam>
        /// <param name="createCommandFunc"></param>
        /// <returns></returns>
        public virtual LinuxCommandResultBase<List<R>> ExecuteCommand<R>(Func<ILinuxCommand> createCommandFunc) where R : ILinuxCommandResultData<R> ,new()
        {
            var resultBase = ExecuteCommand(createCommandFunc);
            R r = new R();
            return r.ParseResultDataList(resultBase);
        }
        /// <summary>
        /// 加载路径
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        Task LoadDirectoryTree(DirectoryNode node);
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="fileStream">上传的文件</param>
        /// <param name="remoteFilePath">服务器路径</param>
        /// <exception cref="Exception"></exception>
        void UploadFile(byte[] fileData, string remoteFilePath);
        public Task UploadFileAsync(byte[] fileData, string remoteFilePath, Action<double> progressCallback = null);
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="remoteFilePath">服务器文件路径</param>
        /// <returns>文件流字符串</returns>
        /// <exception cref="Exception"></exception>
        byte[] DownloadFile(string remoteFilePath);
    }
}
